#include <stdint.h>

#include "vicl.h"

int register_virtual_machine(int idx)
{
    // Register the virtual machine:
    jobHandle = VixHost_RegisterVM(hostHandle,
                                   vmachine[idx].path,
                                   NULL,        // callbackProc
                                   NULL);       // clientData
    err = VixJob_Wait(jobHandle,
                      VIX_PROPERTY_NONE);
    if(VIX_OK != err) 
    {
        return -1;
    }
    
    return 0;
}

int open_virtual_machine(int idx)
{
    if(idx >= MAX_VMS)
        return -1;
        
    if(vmachine[idx].usable == false)
        return -1;
        
    if(register_virtual_machine(idx) == -1)
        return -1;
        
    // Open the virtual machine:
    jobHandle = VixVM_Open(hostHandle,
                           vmachine[idx].path,
                           NULL,        // callbackProc
                           NULL);       // clientData
                           
    err = VixJob_Wait(jobHandle,
                      VIX_PROPERTY_JOB_RESULT_HANDLE,
                      &vmachine[idx].handle,
                      VIX_PROPERTY_NONE);
                      
    if(VIX_OK != err)
       return -1;

    Vix_ReleaseHandle(jobHandle);

    return 0;
}

